import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { ElMessage } from 'element-plus'
export const useTokenStore = defineStore('mytoken', () => {
  //存储token
  const tokenJson = ref('')

  const token = computed(() => {
    try {
      return JSON.stringify(tokenJson.value)
    } catch (err) {
      ElMessage.error('json字符串格式不对,转化对象时失败')
    }
    return JSON.stringify(tokenJson.value || {})
  })

  //function 相当于 actions
  function saveToken(data) {
    tokenJson.value = data
  }

  //向外暴露
  return { token, saveToken }
})
